//
//  ViewController.m
//  17-HomeWork
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "VCTableViewCell.h"
#import "AddViewController.h"
#import "DetailsViewController.h"
#import "PersonModel.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
@interface ViewController () <UITableViewDelegate, UITableViewDataSource, AddPersonDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)NSMutableArray<NSIndexPath *> *selectIndexArr;
@property (nonatomic, strong)NSMutableArray<NSString *>*selectNameArr;
@end

@implementation ViewController
// http://www.jianshu.com/users/31db608214f8/latest_articles
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"联系人";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.editButtonItem.action = @selector(editAction);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickLeftBtn:)];
    [self creatArray];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[VCTableViewCell class] forCellReuseIdentifier:@"cell"];
}
// 数组
- (void)creatArray {
    _selectNameArr = [NSMutableArray array];
    _selectIndexArr = [NSMutableArray array];
    PersonModel *person1 = [[PersonModel alloc] initWithName:@"雷神" imageName:@"雷神" address:@"M4A1" iphone:@"888RMB"];
    PersonModel *person2 = [[PersonModel alloc] initWithName:@"龙啸" imageName:@"龙啸" address:@"手斧" iphone:@"558RMB"];
    PersonModel *person3 = [[PersonModel alloc] initWithName:@"屠龙" imageName:@"屠龙" address:@"尼泊尔" iphone:@"588RMB"];
    PersonModel *person4 = [[PersonModel alloc] initWithName:@"擎天" imageName:@"擎天" address:@"铁锹" iphone:@"588RMB"];
    PersonModel *person5 = [[PersonModel alloc] initWithName:@"毁灭" imageName:@"毁灭" address:@"巴雷特" iphone:@"888RMB"];
    PersonModel *person6 = [[PersonModel alloc] initWithName:@"死神" imageName:@"死神" address:@"M4A1" iphone:@"888RMB"];
     NSArray *array = @[person1, person2, person3, person4, person5, person6];
    _arr = [NSMutableArray arrayWithArray:array];
}
 //  右按钮关联
- (void)editAction {
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
    if (self.editing == NO) {
        [_arr removeObjectsInArray:_selectNameArr];
        [_tableView deleteRowsAtIndexPaths:_selectIndexArr withRowAnimation:UITableViewRowAnimationLeft];
        [_selectNameArr removeAllObjects];
        [_selectIndexArr removeAllObjects];
    }
}
// 左按钮关联
- (void)clickLeftBtn:(UIBarButtonItem *)leftBtn {
    AddViewController *addVC = [[AddViewController alloc] init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
}
- (void)addPersonName:(NSString *)name iphone:(NSString *)iphone text:(NSString *)text Image:(UIImage *)image {
    [_arr addObject:name];
    [_arr addObject:iphone];
    //[_arr addObject:image];
    [_arr addObject:text];
    [_tableView reloadData];
}
// 点击右按钮动画
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
// 判断能否删除
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row ==0) {
        return @"不能删除";
    }
    return @"删除";
}
// 删除
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *rowAction3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [_arr removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }];
    return @[ rowAction3];
}
// tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        [_selectNameArr addObject:_arr[indexPath.row]];
        [_selectIndexArr addObject:indexPath];
    }
    DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
   detailsVC.title = [_arr[indexPath.row ] name];
    detailsVC.personModel = _arr[indexPath.row];
    [self.navigationController pushViewController:detailsVC animated:YES];
}
// 必须实现cell协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
}
// 必须实现cell协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   cell.textLabel.text = [_arr[indexPath.row] name];
    PersonModel *person = _arr[indexPath.row];
    cell.personModel = person;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
